package avicit.bdp.oss.rest;

import avicit.bdp.oss.dto.BucketAuthDto;
import avicit.bdp.oss.dto.BucketDto;
import avicit.bdp.oss.dto.BucketStorageStatsQueryDto;
import avicit.bdp.oss.dto.SysStatsQureyDto;
import avicit.bdp.oss.service.auth.BucketAuthService;
import avicit.bdp.oss.service.bucket.BucketService;
import avicit.bdp.oss.utils.stats.MetricTypeEnum;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2020-11-12
 * @类说明：BucketRest
 * @修改记录：
 * @注意事项：
 * @主要功能：Bucket相关操作Rest接口
 */
@RestController
@RequestMapping("/api/bdp/oss/bucket")
@Api(tags = "bucket操作接口")
public class BucketRest {
    private static final Logger logger = LoggerFactory.getLogger(BucketRest.class);

    @Autowired
    private BucketService bucketService;

    @Autowired
    private BucketAuthService bucketAuthService;

    /**
     * 创建桶
     * @param bucketDto
     * @return
     */
    @PostMapping("/makeBucket")
    @ApiOperation(value = "创建桶")
    public ResponseMsg makeBucket(@RequestBody BucketDto bucketDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.bucketService.makeBucket(bucketDto);

        return responseMsg;
    }

    /**
     * 修改桶策略
     * @param bucketDto
     * @return
     */
    @PostMapping("/updateBucketStrategy")
    @ApiOperation(value = "修改模板")
    public ResponseMsg updateBucketStrategy(@RequestBody BucketDto bucketDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.bucketService.updateBucketStrategy(bucketDto);

        return responseMsg;
    }

    /**
     * 删除桶
     * @param bucketDto
     * @return
     */
    @DeleteMapping("/removeBucket")
    @ApiOperation(value = "删除桶")
    public ResponseMsg deleteBucket(@RequestBody BucketDto bucketDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.bucketService.removeBucket(bucketDto);

        return responseMsg;
    }

    /**
     * 查询bucket名称是否重复
     * @param bucketName
     * @return
     */
    @GetMapping("/checkBucketNameExist")
    @ApiOperation(value = "查询bucket名称是否重复")
    public ResponseMsg<Boolean> checkBucketNameExist(@ApiParam(value = "bucketName", name = "bucketName")  String bucketName) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketService.checkBucketNameExist(bucketName));

        return responseMsg;
    }

    /**
     * 分页查询/模糊查询桶
     * @param sort
     * @param bucketName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getBucketList")
    @ApiOperation(value = "查询bucket列表")
    public ResponseMsg<QueryRespBean<BucketDto>> getBucketList(
            @ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：2，按名称升序：3，按名称倒序：4", name = "sort") @RequestParam(required = false) Integer sort,
            @ApiParam(value = "桶名称", name = "bucketName") @RequestParam(required = false) String bucketName,
            @ApiParam(value = "当前页", name = "pageNo", required = true)  @RequestParam(defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页显示条数", name = "pageSize", required = true) @RequestParam(defaultValue = "10") Integer pageSize) {
        ResponseMsg<QueryRespBean<BucketDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketService.getBucketList(sort, bucketName, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 新增授权信息
     * @param authInfoDto
     * @return
     */
    @PostMapping("/addBucketAuth")
    @ApiOperation(value = "新增授权")
    public ResponseMsg addBucketAuth(@RequestBody BucketAuthDto authInfoDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.bucketAuthService.addBucketAuth(authInfoDto);

        return responseMsg;
    }

    /**
     * 修改授权
     * @param authInfoDto
     * @return
     */
    @PostMapping("/updateBucketAuth")
    @ApiOperation(value = "修改授权")
    public ResponseMsg updateBucketAuth(@RequestBody BucketAuthDto authInfoDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.bucketAuthService.updateBucketAuth(authInfoDto);

        return responseMsg;
    }

    /**
     * 删除授权
     * @param authInfoDto
     * @return
     */
    @DeleteMapping("/deleteBucketAuth")
    @ApiOperation(value = "删除授权")
    public ResponseMsg deleteBucketAuth(@RequestBody BucketAuthDto authInfoDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.bucketAuthService.deleteBucketAuth(authInfoDto);

        return responseMsg;
    }

    /**
     * 分页查询/模糊查询授权列表
     * @param sort
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getBucketAuthList")
    @ApiOperation(value = "查询授权列表")
    public ResponseMsg<QueryRespBean<BucketAuthDto>> getBucketAuthList(
            @ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：", name = "sort") @RequestParam(required = false) Integer sort,
            @ApiParam(value = "当前页", name = "pageNo", required = true)  @RequestParam(defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页显示条数", name = "pageSize", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "桶ID", name = "id") @RequestParam(required = true) String id) {
        ResponseMsg<QueryRespBean<BucketAuthDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketAuthService.getBucketAuthList(sort, pageNo, pageSize, id));

        return responseMsg;
    }

    /**
     * 获取系统统计信息，包括bucket总量、存储总量、对象总量
     * @return
     */
    @GetMapping("/getSysStats")
    @ApiOperation(value = "查询系统统计信息")
    public ResponseMsg<SysStatsQureyDto> getSysStats() {
        ResponseMsg<SysStatsQureyDto> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketService.getSysStats());

        return responseMsg;
    }

    /**
     * 获取bucket存量统计信息，包括存储总量、对象总量
     * @return
     */
    @GetMapping("/getBucketStorageStats")
    @ApiOperation(value = "获取bucket存量统计信息")
    public ResponseMsg<BucketStorageStatsQueryDto> getBucketStorageStats(
            @ApiParam(value = "桶Id", name = "bucketId", required = true) @RequestParam(required = true) String bucketId) {
        ResponseMsg<BucketStorageStatsQueryDto> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketService.getBucketStorageStats(bucketId));

        return responseMsg;
    }

    /**
     * 获取bucket指标统计信息，包括本月读取量、本月写入量、本月PUT请求数、本月GET请求数、今日读取量、今日写入量、今日PUT请求数、今日GET请求数
     * @param bucketId
     * @return
     */
    @GetMapping("/getBucketMetricStats")
    @ApiOperation(value = "查询Bucket概要统计信息")
    public ResponseMsg<Object> getBucketMetricStats(
            @ApiParam(value = "桶Id", name = "bucketId", required = true) @RequestParam(required = true) String bucketId,
            @ApiParam(value = "指标类型", name = "metricType", required = true) @RequestParam(required = true) String metricType) {
        ResponseMsg<Object> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketService.getBucketMetricStats(bucketId, MetricTypeEnum.getTypeEnum(metricType)));

        return responseMsg;
    }

    /**
     * 获取系统统计信息，包括bucket总量、存储总量、对象总量
     * @return
     */
    @GetMapping("/getVersion")
    @ApiOperation(value = "查询版本号")
    public ResponseMsg<String> getVersion() {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.bucketService.getVersion());

        return responseMsg;
    }
}
